package com.atguigu.tingshu.payment.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.account.AccountFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.model.payment.PaymentInfo;
import com.atguigu.tingshu.order.client.OrderFeignClient;
import com.atguigu.tingshu.payment.mapper.PaymentInfoMapper;
import com.atguigu.tingshu.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@SuppressWarnings({"all"})
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {

    @Autowired
    private PaymentInfoMapper paymentInfoMapper;

    @Autowired
    private OrderFeignClient orderFeignClient;

    @Autowired
    private AccountFeignClient accountFeignClient;

    /**
     * 保存本地交易记录
     *
     * @param paymentType 支付类型：1301-订单 1302-充值
     * @param orderNo     订单编号
     * @return
     */
    @Override
    public PaymentInfo savePaymentInfo(String paymentType, String orderNo) {
        //1.根据订单编号查询本地交易记录，如果存在则直接返回
        LambdaQueryWrapper<PaymentInfo> paymentInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        paymentInfoLambdaQueryWrapper.eq(PaymentInfo::getOrderNo, orderNo);
        PaymentInfo paymentInfo = paymentInfoMapper.selectOne(paymentInfoLambdaQueryWrapper);
        if (paymentInfo != null) {
            return paymentInfo;
        }

        //2.封装本地交易记录对象完成保存
        //2.1 初始化本地交易对象
        paymentInfo = new PaymentInfo();
        //2.2 封装本地交易信息
        paymentInfo.setPaymentType(paymentType);
        paymentInfo.setOrderNo(orderNo);
        //支付状态：1401-未支付 1402-已支付
        paymentInfo.setPaymentStatus(SystemConstant.PAYMENT_STATUS_UNPAID);
        paymentInfo.setPayWay(SystemConstant.ORDER_PAY_WAY_WEIXIN);

        //2.3 判断支付类型-如果是订单，根据订单编号远程调用订单服务获取订单信息
        if (SystemConstant.PAYMENT_TYPE_ORDER.equals(paymentType)) {
            //2.3.1 远程调用订单服务获取订单信息
            OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderNo).getData();
            Assert.notNull(orderInfo, "订单{}不存在", orderNo);
            String orderStatus = orderInfo.getOrderStatus();
            if (!SystemConstant.ORDER_STATUS_UNPAID.equals(orderStatus)) {
                throw new RuntimeException("订单已支付或者订单已取消");
            }

            //2.3.2 封装本地交易记录中的内容和金额
            Long userId = orderInfo.getUserId();
            BigDecimal orderAmount = orderInfo.getOrderAmount();
            String orderTitle = orderInfo.getOrderTitle();

            paymentInfo.setUserId(userId);
            paymentInfo.setAmount(orderAmount);
            paymentInfo.setContent(orderTitle);

        }

        //2.4 判断支付类型-如果是充值，根据充值订单编号远程调用账户服务获取充值信息
        if (SystemConstant.PAYMENT_TYPE_RECHARGE.equals(paymentType)) {
            //2.4.1 远程调用充值服务获取充值信息
            RechargeInfo rechargeInfo = accountFeignClient.getRechargeInfo(orderNo).getData();
            Assert.notNull(rechargeInfo, "充值订单{}不存在", orderNo);
            //2.4.2 判断充值状态
            String rechargeStatus = rechargeInfo.getRechargeStatus();
            if (!SystemConstant.ORDER_STATUS_UNPAID.equals(rechargeStatus)) {
                throw new RuntimeException("充值订单已支付或者充值订单已取消");
            }

            //2.4.3 封装本地交易记录中的内容和金额
            Long userId = rechargeInfo.getUserId();
            BigDecimal rechargeAmount = rechargeInfo.getRechargeAmount();

            paymentInfo.setUserId(userId);
            paymentInfo.setAmount(rechargeAmount);
            paymentInfo.setContent("充值：" + rechargeAmount);

        }

//        //微信端交易订单号
//        paymentInfo.setOutTradeNo();
//        //回调时间
//        paymentInfo.setCallbackTime();
//        //回调内容 待用户付款成功以后，商户系统才会收到回调消息
//        paymentInfo.setCallbackContent();

        //3.保存或者新增本地交易记录
        paymentInfoMapper.insert(paymentInfo);
//        this.save(paymentInfo);
        return paymentInfo;
    }
}
